<%@ page contentType="text/html; charset=UTF-8"%>
<table border="1" style="border-collapse: collapse; width: 100%; margin-top: 10px;">
	<tr class="d" >
		<th COLSPAN="5">socket设置</th>
	</tr>
	<tr class="d">
		<td>端口：</td>
		<td style="padding-left: 5px"><input id="socketPortText" type="text" style="width: 205px"  />
		</td>
		<td id="socketState"></td>
		<!-- <td ><input type="checkbox" id= "checkStartSockeServer"  onclick="checkStartSockeServer()"   />随服务启动</td> -->
		<td style="text-align: center;"><button id="starSocketBtn" onclick="startSocketServer()">启动socket服务</button>
		<button  id="closeSocketBtn" onclick="closeSocketServer()">关闭socket服务</button>
		</td>
	</tr>
</table>
<table border="0" style="border-collapse: collapse; width: 100%;margin-top: 20px">
	<tr class="d">
		<td >终端在线列表:</td>	
		<td ></td>
		<td style="text-align:right;"><button onclick="getUsers()">刷新</button></td>
	</tr>

</table>
<table id="userTable" border="1" style="border-collapse: collapse; width: 100%;">


</table>
<script>



	getUsers();
	
	getSocketServerState();
	function getSocketServerState(){
	$.post("settingSocketAction", {
			"actionType" : "getSocketServerState"
		}, function(data) {
		
		
			if(undefined==data.er){
			
				$("#closeSocketBtn").show();
				$("#starSocketBtn").hide();
				$("#socketState").html("状态：运行中");
				$("#socketPortText").val(data.port);
			
			}else{
			
				$("#closeSocketBtn").hide();
				$("#starSocketBtn").show();
				$("#socketState").html("状态：未运行");
				
			}
		
		},"json");
	}
	
	


  function getUsers(){
	  
		$.post("settingSocketAction", {
			"actionType" : "getUsers"
		}, function(data) {
			
			if(undefined==data.er){
			
				if(data.length!=0){
					var html = "";
					for(var i =0 ; i <data.length;i++){
					var obj = data[i];
					html += "<tr class="d" ><td >"+obj.ip+"</td ><td > "+obj.deviceType+"</td><td  style='text-align: right ;'>操作</td></tr>";
					}
					inserUserTable(html);
			
				
				}else{
					
				inserUserTable("<tr class='d' ><td COLSPAN='3' > 没有连接终端在线</td></tr>");
				
				}
				
		
			}else{
				
				inserUserTable("<tr class='d' ><td COLSPAN='3'> "+data.er+"</td></tr>");
				
			}
			
			
			
		},"json");
	  
  }
  
  function inserUserTable(val){
  
  	var html = "<tr class='d'><th >终端IP</th ><th > 终端类型</th><th  style='text-align: right ;'>操作</th></tr>	";
		
		html+=val;
  	
  $("#userTable").html(html);
  
  }
  
  function startSocketServer(){
	  
	  
	  var socketPort = $("#socketPortText").val();
	  
	 
	  
	  if(socketPort==""||socketPort==null){
		  alert("端口不能为空");
		  return;
	  }
	  
		$.post("settingSocketAction", {
			"actionType" : "startSocketServer","socketPort" : socketPort
		}, function(data) {
			
			if(undefined==data.er){
				getSocketServerState();
				alert(data.msg);
			}else{
				alert(data.er);
			}
		},"json");
	  
  }
	 
  function closeSocketServer(){
	  
	  $.post("settingSocketAction", {
			"actionType" : "closeSocketServer"
		}, function(data) {
			
			if(undefined==data.er){
			
				getSocketServerState();
				alert(data.msg);
			}else{
				alert(data.er);
			}
		},"json");

  }
  
  
  function checkStartSockeServer(){
  
  
	var state = document.getElementById("checkStartSockeServer").checked;
  
   $.post("settingSocketAction", {
			"actionType" : "checkStartSockeServer","state":""+state
		}, function(data) {
			
			if(data==null||undefined==data.msg){
					document.getElementById("checkStartSockeServer").checked = false;
				
			}else{
				alert(data.msg);
			}
		},"json");
		
  }

</script>